/*
 * SignAboutManager.java
 * business
 *
 * Created by SunDing on 2016/12/29.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.manager;

import android.animation.ValueAnimator;
import android.app.Activity;
import android.app.Dialog;
import android.os.Handler;
import android.view.animation.LinearInterpolator;

import com.yidiandao.business.constant.Common;
import com.yidiandao.business.data.model.ISignModel;
import com.yidiandao.business.data.response.RaffleResponse;
import com.yidiandao.business.data.response.SignInfoResponse;
import com.yidiandao.business.ui.adapter.SignRuleItemAdapter;
import com.yidiandao.business.ui.widget.dialog.PrizeDialog;
import com.yidiandao.business.ui.widget.dialog.UnPrizeDialog;
import com.yidiandao.business.ui.widget.view.NoScrollRecycleView;

import java.util.ArrayList;
import java.util.List;

/** 签到抽奖 */
public class SignAboutManager {

    private SignRuleItemAdapter mAdapter;
    private NoScrollRecycleView mRecyclerView;
    private Handler mHandler = new Handler();
    /** 记录RecycleView上次滚动距离 */
    private int value;
    /** 记录RecycleView滚动条数 */
    private int position;
    /** 用于存储所有的中奖信息 */
    private List<SignInfoResponse.WinnerBean> mTotalWinnerList = new ArrayList<>();
    /** 从服务器获取的获奖信息 */
    private List<SignInfoResponse.WinnerBean> mWinnerList = new ArrayList<>();

    private String mType;
    private String mPrizePicture;
    private String mPrizeName;
    private PrizeDialog mPrizeDialog;
    private UnPrizeDialog mUnPrizeDialog;
    private Activity mActivity;

    public SignAboutManager(Activity activity) {
        mActivity = activity;
    }

    /** 中奖信息开始定时滚动 */
    public void startWinnerlistScroll(
            List<SignInfoResponse.WinnerBean> winnerList,
            SignRuleItemAdapter adapter,
            NoScrollRecycleView recyclerView) {
        if (null != winnerList && winnerList.size() > 0) {
            value = 0;
            position = 0;
            mTotalWinnerList.clear();
            mWinnerList.clear();
            mAdapter = adapter;
            mRecyclerView = recyclerView;
            mWinnerList.addAll(winnerList);
            mTotalWinnerList.addAll(mWinnerList);
            mAdapter.setData(mTotalWinnerList);
            if (mTotalWinnerList.size() >= Common.WINNER_COUNT) {
                smoothToPosition();
            }
        }
    }

    /** 开始滚动 */
    private void smoothToPosition() {
        mHandler.postDelayed(
                new Runnable() {
                    @Override
                    public void run() {
                        ValueAnimator animator = ValueAnimator.ofInt(Common.SIGNWINNER_ITEMHEIGHT);
                        animator.setDuration(500);
                        animator.setInterpolator(new LinearInterpolator());
                        animator.addUpdateListener(
                                new ValueAnimator.AnimatorUpdateListener() {
                                    @Override
                                    public void onAnimationUpdate(ValueAnimator animation) {
                                        int currentValue = (int) animation.getAnimatedValue();
                                        mRecyclerView.scrollBy(0, currentValue - value);
                                        value = currentValue;
                                    }
                                });
                        value = 0;
                        animator.start();
                        smoothToPosition();
                        position++;
                        if (position + Common.WINNER_COUNT == mTotalWinnerList.size()) {
                            mTotalWinnerList.addAll(mWinnerList);
                            mAdapter.setData(mTotalWinnerList);
                        }
                    }
                },
                2000);
    }

    /** 设置抽奖结果 */
    public void setRaffleInfo(RaffleResponse.DataBean data) {
        mPrizeName = data.getPrizeName();
        mPrizePicture = data.getPrizePicture();
        mType = data.getType();
    }

    public void destroyHandler() {
        mHandler.removeCallbacksAndMessages(null);
    }

    /** @param drawCount 剩余抽奖次数 */
    public void showDialog(int drawCount) {
        switch (mType) {
            case ISignModel.RaffleType.SUCCESS:
                if (null == mPrizeDialog) mPrizeDialog = new PrizeDialog(mActivity);
                show(mPrizeDialog);
                mPrizeDialog.setPrizeInfo(mPrizeName, mPrizePicture);
                break;
            case ISignModel.RaffleType.FAILED:
                if (null == mUnPrizeDialog) mUnPrizeDialog = new UnPrizeDialog(mActivity);
                show(mUnPrizeDialog);
                mUnPrizeDialog.setCount(drawCount, mPrizePicture);
                break;
        }
    }

    private void show(Dialog dialog) {
        if (null != dialog && !dialog.isShowing() && null != mActivity) dialog.show();
    }
}
